<template>
  <div>
    <e-table
      ref="eTable"
      @getList="handleGetList"
      :tableData="tableData"
      :columns="columns"
      :loading="loading"
      :total="total"
    >
      <el-table-column slot="action" fixed="right" width="50" align="center">
        <template slot-scope="scope">
          <el-dropdown>
            <a class="el-dropdown-link">
              <i class="el-icon-menu"></i>
            </a>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item
                v-if="$auth('assets:update')"
                @click.native="handleEdit(true, scope.row)"
              >
                <a>编辑</a>
              </el-dropdown-item>
              <el-dropdown-item
                v-if="$auth('assets:delete')"
                @click.native="handleDelete(scope.row)"
              >
                <a class="red">删除</a>
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </e-table>
  </div>
</template>

<script>
import { assetsAntennaPage, accessAntennaDelete } from '@/api/assetsNew'
import { openNotification } from '@/utils/util'

export default {
  data() {
    return {
      tableData: [],
      total: 1,
      loading: false,
      columns: [
        {
          label: '门禁设备天线名称',
          prop: 'antennaName'
        },
        {
          label: '天线序号',
          prop: 'antennaNo'
        },
        {
          slot: 'action'
        }
      ],
      queryParam: {
        pageNo: 1,
        pageSize: 10
      },
      mdl: null
    }
  },
  methods: {
    edit(row) {
      this.mdl = Object.assign({}, row)
      this.handleGetList({ accessId: row.accessId })
    },
    async handleGetList(param = {}) {
      this.loading = true
      this.queryParam = Object.assign(this.queryParam, param)
      const res = await assetsAntennaPage(this.queryParam)

      if (res && res.data.records.length > 0) {
        const {
          state,
          data: { records, total }
        } = res
        this.tableData = records
        this.total = total
      } else {
        this.tableData = []
        this.total = 1
      }
      this.loading = false
    },
    handleEdit(bool, row) {
      this.$emit('edit', bool, row)
    },
    refresh() {
      this.handleGetList({ accessId: this.mdl.accessId })
    },
    handleDelete(row) {
      console.log('row', row)
      const _this = this
      this.$confirm({
        title: '删除操作',
        content: `您确定删除[${row.antennaName}]这条记录吗？`,
        okText: '确定',
        okType: 'danger',
        cancelText: '取消',
        centered: true,
        async onOk() {
          // 调用删除接口，刷新表格数据
          const res = await accessAntennaDelete(row.antennaId, {})
          openNotification('success', '删除成功')
          const index = _this.tableData.findIndex(v => v.accessId === row.accessId)
          _this.tableData.splice(index, 1)
        }
      })
    }
  }
}
</script>
